/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JIFChamadoAdmin.java
 *
 * Created on 27/08/2011, 17:14:23
 */
package view.admin;

import control.ChamadoControl;
import control.Sessao;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import model.Chamado;
import model.Pessoa;
import model.Resposta;
import util.CellRenderer;
import view.IJIFSimpleFactory;

/**
 *
 * @author Felipe
 */
public class JIFChamadoAdmin extends JInternalFrame implements IJIFSimpleFactory {
    private Pessoa usuarioLogado;
    private ChamadoControl controller;
    
    /** Creates new form JIFChamadoAdmin */
    public JIFChamadoAdmin() {         
        controller = ChamadoControl.getControl();
        usuarioLogado = Sessao.getInstance().getUsuarioLogado();
        initComponents();        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jTabbedPane1 = new javax.swing.JTabbedPane();
        pnlNAtribuidos = new javax.swing.JPanel();
        pnlNAtriSuperior = new javax.swing.JPanel();
        pnlBusca = new javax.swing.JPanel();
        cbbBusca = new javax.swing.JComboBox();
        btnBuscar = new javax.swing.JButton();
        txtBusca = new javax.swing.JTextField();
        pnlJtb = new javax.swing.JPanel();
        jtbSuperior = new javax.swing.JToolBar();
        btnVisualizarNAtribuido = new javax.swing.JButton();
        btnAutoAtribuir = new javax.swing.JButton();
        btnInserir = new javax.swing.JButton();
        btnEncerrarNAtri = new javax.swing.JButton();
        pnlSuperiorCentro = new javax.swing.JPanel();
        pnlSuperiorCentroDivisor = new javax.swing.JPanel();
        pnlNAtriOeste = new javax.swing.JPanel();
        pnlNAtriLeste = new javax.swing.JPanel();
        pnlNAtriSul = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblNAtribuidos = new javax.swing.JTable();
        pnlEmAndamento = new javax.swing.JPanel();
        pnlEASuperior = new javax.swing.JPanel();
        pnlEABusca = new javax.swing.JPanel();
        cbbBuscaEA = new javax.swing.JComboBox();
        btnBuscarEA = new javax.swing.JButton();
        txtBuscaEA = new javax.swing.JTextField();
        pnlEAJtb = new javax.swing.JPanel();
        jtbSuperiorEA = new javax.swing.JToolBar();
        btnVisualizarEA = new javax.swing.JButton();
        btnResponderEA = new javax.swing.JButton();
        btnIniciarIntervencao = new javax.swing.JButton();
        btnEncerrarIntervencao = new javax.swing.JButton();
        btnEncerrarEA = new javax.swing.JButton();
        pnlEASupCen = new javax.swing.JPanel();
        pnlSupCenDiEA = new javax.swing.JPanel();
        pnlEASul = new javax.swing.JPanel();
        pnlEAOeste = new javax.swing.JPanel();
        pnlEALeste = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblEA = new javax.swing.JTable();
        pnlEncerrados = new javax.swing.JPanel();
        pnlEnceSuperior = new javax.swing.JPanel();
        pnlEncBusca = new javax.swing.JPanel();
        cbbBuscaEnc = new javax.swing.JComboBox();
        btnBuscarEnc = new javax.swing.JButton();
        txtBuscaEnc = new javax.swing.JTextField();
        pnlEncJtb = new javax.swing.JPanel();
        jtbSupEnc = new javax.swing.JToolBar();
        btnVisuEnce = new javax.swing.JButton();
        pnlEncSupCent = new javax.swing.JPanel();
        pnlEncDivisor = new javax.swing.JPanel();
        pnlEnceLeste = new javax.swing.JPanel();
        pnlEnceOeste = new javax.swing.JPanel();
        pnlEnceSul = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblEncerrados = new javax.swing.JTable();

        setClosable(true);
        setIconifiable(true);
        setTitle("Controle de chamado");
        setPreferredSize(new java.awt.Dimension(745, 586));

        pnlNAtribuidos.setLayout(new java.awt.BorderLayout());

        pnlNAtriSuperior.setMinimumSize(new java.awt.Dimension(364, 50));
        pnlNAtriSuperior.setPreferredSize(new java.awt.Dimension(613, 50));
        pnlNAtriSuperior.setLayout(new java.awt.BorderLayout());

        pnlBusca.setMaximumSize(new java.awt.Dimension(430, 20));
        pnlBusca.setMinimumSize(new java.awt.Dimension(430, 20));
        pnlBusca.setPreferredSize(new java.awt.Dimension(430, 20));
        pnlBusca.setLayout(new javax.swing.BoxLayout(pnlBusca, javax.swing.BoxLayout.LINE_AXIS));

        cbbBusca.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbbBusca.setMaximumSize(new java.awt.Dimension(200, 20));
        cbbBusca.setMinimumSize(new java.awt.Dimension(200, 20));
        cbbBusca.setPreferredSize(new java.awt.Dimension(200, 20));
        pnlBusca.add(cbbBusca);

        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/buscar2.png"))); // NOI18N
        btnBuscar.setToolTipText("Procurar");
        btnBuscar.setMaximumSize(new java.awt.Dimension(30, 20));
        btnBuscar.setMinimumSize(new java.awt.Dimension(30, 20));
        btnBuscar.setOpaque(false);
        btnBuscar.setPreferredSize(new java.awt.Dimension(30, 20));
        pnlBusca.add(btnBuscar);

        txtBusca.setMaximumSize(new java.awt.Dimension(200, 20));
        txtBusca.setMinimumSize(new java.awt.Dimension(200, 20));
        txtBusca.setPreferredSize(new java.awt.Dimension(200, 20));
        pnlBusca.add(txtBusca);

        pnlNAtriSuperior.add(pnlBusca, java.awt.BorderLayout.EAST);

        pnlJtb.setLayout(new java.awt.BorderLayout());

        jtbSuperior.setBorder(null);
        jtbSuperior.setFloatable(false);
        jtbSuperior.setRollover(true);
        jtbSuperior.setOpaque(false);

        btnVisualizarNAtribuido.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/view1.gif"))); // NOI18N
        btnVisualizarNAtribuido.setToolTipText("Visualizar chamado");
        btnVisualizarNAtribuido.setFocusable(false);
        btnVisualizarNAtribuido.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnVisualizarNAtribuido.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jtbSuperior, org.jdesktop.beansbinding.ELProperty.create("false"), btnVisualizarNAtribuido, org.jdesktop.beansbinding.BeanProperty.create("borderPainted"));
        bindingGroup.addBinding(binding);

        btnVisualizarNAtribuido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVisualizarNAtribuidoActionPerformed(evt);
            }
        });
        jtbSuperior.add(btnVisualizarNAtribuido);

        btnAutoAtribuir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/executivo2.png"))); // NOI18N
        btnAutoAtribuir.setToolTipText("Atribuir chamado a si mesmo");
        btnAutoAtribuir.setFocusable(false);
        btnAutoAtribuir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnAutoAtribuir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jtbSuperior, org.jdesktop.beansbinding.ELProperty.create("false"), btnAutoAtribuir, org.jdesktop.beansbinding.BeanProperty.create("borderPainted"));
        bindingGroup.addBinding(binding);

        btnAutoAtribuir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAutoAtribuirActionPerformed(evt);
            }
        });
        jtbSuperior.add(btnAutoAtribuir);

        btnInserir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/user2.png"))); // NOI18N
        btnInserir.setToolTipText("Atribuir técnico ao chamado");
        btnInserir.setFocusable(false);
        btnInserir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnInserir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jtbSuperior, org.jdesktop.beansbinding.ELProperty.create("false"), btnInserir, org.jdesktop.beansbinding.BeanProperty.create("borderPainted"));
        bindingGroup.addBinding(binding);

        btnInserir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnInserirActionPerformed(evt);
            }
        });
        jtbSuperior.add(btnInserir);

        btnEncerrarNAtri.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/joia.png"))); // NOI18N
        btnEncerrarNAtri.setToolTipText("Finalizar chamado");
        btnEncerrarNAtri.setFocusable(false);
        btnEncerrarNAtri.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEncerrarNAtri.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jtbSuperior, org.jdesktop.beansbinding.ELProperty.create("false"), btnEncerrarNAtri, org.jdesktop.beansbinding.BeanProperty.create("borderPainted"));
        bindingGroup.addBinding(binding);

        btnEncerrarNAtri.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEncerrarNAtriActionPerformed(evt);
            }
        });
        jtbSuperior.add(btnEncerrarNAtri);

        pnlJtb.add(jtbSuperior, java.awt.BorderLayout.CENTER);

        pnlNAtriSuperior.add(pnlJtb, java.awt.BorderLayout.WEST);

        pnlSuperiorCentro.setLayout(new javax.swing.BoxLayout(pnlSuperiorCentro, javax.swing.BoxLayout.LINE_AXIS));

        pnlSuperiorCentroDivisor.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        pnlSuperiorCentroDivisor.setMaximumSize(new java.awt.Dimension(1000, 20));
        pnlSuperiorCentroDivisor.setOpaque(false);
        pnlSuperiorCentroDivisor.setPreferredSize(new java.awt.Dimension(1000, 20));
        pnlSuperiorCentro.add(pnlSuperiorCentroDivisor);

        pnlNAtriSuperior.add(pnlSuperiorCentro, java.awt.BorderLayout.CENTER);

        pnlNAtribuidos.add(pnlNAtriSuperior, java.awt.BorderLayout.PAGE_START);
        pnlNAtribuidos.add(pnlNAtriOeste, java.awt.BorderLayout.WEST);
        pnlNAtribuidos.add(pnlNAtriLeste, java.awt.BorderLayout.EAST);
        pnlNAtribuidos.add(pnlNAtriSul, java.awt.BorderLayout.PAGE_END);

        tblNAtribuidos.setAutoCreateRowSorter(true);
        tblNAtribuidos.setModel(controller.getChamadosNAtriTableModel());
        tblNAtribuidos.setRowHeight(20);
        ajustarColunasNaoAtribuido();
        tblNAtribuidos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblNAtribuidosMouseClicked(evt);
            }
        });
        tblNAtribuidos.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tblNAtribuidosKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tblNAtribuidos);

        pnlNAtribuidos.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab("Não atribuídos", pnlNAtribuidos);

        pnlEmAndamento.setLayout(new java.awt.BorderLayout());

        pnlEASuperior.setMinimumSize(new java.awt.Dimension(364, 50));
        pnlEASuperior.setPreferredSize(new java.awt.Dimension(613, 50));
        pnlEASuperior.setLayout(new java.awt.BorderLayout());

        pnlEABusca.setLayout(new javax.swing.BoxLayout(pnlEABusca, javax.swing.BoxLayout.LINE_AXIS));

        cbbBuscaEA.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbbBuscaEA.setMaximumSize(new java.awt.Dimension(200, 20));
        cbbBuscaEA.setMinimumSize(new java.awt.Dimension(200, 20));
        cbbBuscaEA.setPreferredSize(new java.awt.Dimension(200, 20));
        pnlEABusca.add(cbbBuscaEA);

        btnBuscarEA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/buscar2.png"))); // NOI18N
        btnBuscarEA.setToolTipText("Procurar");
        btnBuscarEA.setMaximumSize(new java.awt.Dimension(30, 20));
        btnBuscarEA.setMinimumSize(new java.awt.Dimension(30, 20));
        btnBuscarEA.setOpaque(false);
        btnBuscarEA.setPreferredSize(new java.awt.Dimension(30, 20));
        pnlEABusca.add(btnBuscarEA);

        txtBuscaEA.setMaximumSize(new java.awt.Dimension(200, 20));
        txtBuscaEA.setMinimumSize(new java.awt.Dimension(200, 20));
        txtBuscaEA.setPreferredSize(new java.awt.Dimension(200, 20));
        pnlEABusca.add(txtBuscaEA);

        pnlEASuperior.add(pnlEABusca, java.awt.BorderLayout.EAST);

        pnlEAJtb.setLayout(new java.awt.BorderLayout());

        jtbSuperiorEA.setBorder(null);
        jtbSuperiorEA.setFloatable(false);
        jtbSuperiorEA.setRollover(true);
        jtbSuperiorEA.setOpaque(false);

        btnVisualizarEA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/view1.gif"))); // NOI18N
        btnVisualizarEA.setToolTipText("Visualizar chamado em andamento");
        btnVisualizarEA.setFocusable(false);
        btnVisualizarEA.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnVisualizarEA.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jtbSuperiorEA, org.jdesktop.beansbinding.ELProperty.create("false"), btnVisualizarEA, org.jdesktop.beansbinding.BeanProperty.create("borderPainted"));
        bindingGroup.addBinding(binding);

        btnVisualizarEA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVisualizarEAActionPerformed(evt);
            }
        });
        jtbSuperiorEA.add(btnVisualizarEA);

        btnResponderEA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/responder1.png"))); // NOI18N
        btnResponderEA.setToolTipText("Responder chamado");
        btnResponderEA.setFocusable(false);
        btnResponderEA.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnResponderEA.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jtbSuperiorEA, org.jdesktop.beansbinding.ELProperty.create("false"), btnResponderEA, org.jdesktop.beansbinding.BeanProperty.create("borderPainted"));
        bindingGroup.addBinding(binding);

        btnResponderEA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResponderEAActionPerformed(evt);
            }
        });
        jtbSuperiorEA.add(btnResponderEA);

        btnIniciarIntervencao.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/relogio.png"))); // NOI18N
        btnIniciarIntervencao.setToolTipText("Iniciar intervenção");
        btnIniciarIntervencao.setFocusable(false);
        btnIniciarIntervencao.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnIniciarIntervencao.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jtbSuperiorEA, org.jdesktop.beansbinding.ELProperty.create("false"), btnIniciarIntervencao, org.jdesktop.beansbinding.BeanProperty.create("borderPainted"));
        bindingGroup.addBinding(binding);

        btnIniciarIntervencao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIniciarIntervencaoActionPerformed(evt);
            }
        });
        jtbSuperiorEA.add(btnIniciarIntervencao);

        btnEncerrarIntervencao.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/fim5.png"))); // NOI18N
        btnEncerrarIntervencao.setToolTipText("Encerrar intervenção");
        btnEncerrarIntervencao.setFocusable(false);
        btnEncerrarIntervencao.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEncerrarIntervencao.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jtbSuperiorEA, org.jdesktop.beansbinding.ELProperty.create("false"), btnEncerrarIntervencao, org.jdesktop.beansbinding.BeanProperty.create("borderPainted"));
        bindingGroup.addBinding(binding);

        btnEncerrarIntervencao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEncerrarIntervencaoActionPerformed(evt);
            }
        });
        jtbSuperiorEA.add(btnEncerrarIntervencao);

        btnEncerrarEA.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/joia.png"))); // NOI18N
        btnEncerrarEA.setToolTipText("Finalizar chamado");
        btnEncerrarEA.setFocusable(false);
        btnEncerrarEA.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnEncerrarEA.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jtbSuperiorEA, org.jdesktop.beansbinding.ELProperty.create("false"), btnEncerrarEA, org.jdesktop.beansbinding.BeanProperty.create("borderPainted"));
        bindingGroup.addBinding(binding);

        btnEncerrarEA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEncerrarEAActionPerformed(evt);
            }
        });
        jtbSuperiorEA.add(btnEncerrarEA);

        pnlEAJtb.add(jtbSuperiorEA, java.awt.BorderLayout.CENTER);

        pnlEASuperior.add(pnlEAJtb, java.awt.BorderLayout.WEST);

        pnlEASupCen.setLayout(new javax.swing.BoxLayout(pnlEASupCen, javax.swing.BoxLayout.LINE_AXIS));

        pnlSupCenDiEA.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        pnlSupCenDiEA.setMaximumSize(new java.awt.Dimension(1000, 20));
        pnlSupCenDiEA.setOpaque(false);
        pnlSupCenDiEA.setPreferredSize(new java.awt.Dimension(1000, 20));
        pnlEASupCen.add(pnlSupCenDiEA);

        pnlEASuperior.add(pnlEASupCen, java.awt.BorderLayout.CENTER);

        pnlEmAndamento.add(pnlEASuperior, java.awt.BorderLayout.PAGE_START);
        pnlEmAndamento.add(pnlEASul, java.awt.BorderLayout.SOUTH);
        pnlEmAndamento.add(pnlEAOeste, java.awt.BorderLayout.WEST);
        pnlEmAndamento.add(pnlEALeste, java.awt.BorderLayout.EAST);

        tblEA.setModel(controller.getChamadosEATableModel());
        tblEA.setRowHeight(20);
        ajustarColunasEmAndamento();
        jScrollPane3.setViewportView(tblEA);

        pnlEmAndamento.add(jScrollPane3, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab("Em andamento", pnlEmAndamento);

        pnlEncerrados.setLayout(new java.awt.BorderLayout());

        pnlEnceSuperior.setMinimumSize(new java.awt.Dimension(364, 50));
        pnlEnceSuperior.setPreferredSize(new java.awt.Dimension(613, 50));
        pnlEnceSuperior.setLayout(new java.awt.BorderLayout());

        pnlEncBusca.setMaximumSize(new java.awt.Dimension(430, 20));
        pnlEncBusca.setMinimumSize(new java.awt.Dimension(430, 20));
        pnlEncBusca.setPreferredSize(new java.awt.Dimension(430, 20));
        pnlEncBusca.setLayout(new javax.swing.BoxLayout(pnlEncBusca, javax.swing.BoxLayout.LINE_AXIS));

        cbbBuscaEnc.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbbBuscaEnc.setMaximumSize(new java.awt.Dimension(200, 20));
        cbbBuscaEnc.setMinimumSize(new java.awt.Dimension(200, 20));
        cbbBuscaEnc.setPreferredSize(new java.awt.Dimension(200, 20));
        pnlEncBusca.add(cbbBuscaEnc);

        btnBuscarEnc.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/buscar2.png"))); // NOI18N
        btnBuscarEnc.setToolTipText("Procurar");
        btnBuscarEnc.setMaximumSize(new java.awt.Dimension(30, 20));
        btnBuscarEnc.setMinimumSize(new java.awt.Dimension(30, 20));
        btnBuscarEnc.setPreferredSize(new java.awt.Dimension(30, 20));
        pnlEncBusca.add(btnBuscarEnc);

        txtBuscaEnc.setMaximumSize(new java.awt.Dimension(200, 20));
        txtBuscaEnc.setMinimumSize(new java.awt.Dimension(200, 20));
        txtBuscaEnc.setPreferredSize(new java.awt.Dimension(200, 20));
        pnlEncBusca.add(txtBuscaEnc);

        pnlEnceSuperior.add(pnlEncBusca, java.awt.BorderLayout.EAST);

        pnlEncJtb.setLayout(new java.awt.BorderLayout());

        jtbSupEnc.setBorder(null);
        jtbSupEnc.setFloatable(false);
        jtbSupEnc.setRollover(true);
        jtbSupEnc.setOpaque(false);

        btnVisuEnce.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/i16x16/view1.gif"))); // NOI18N
        btnVisuEnce.setToolTipText("Visualizar chamado");
        btnVisuEnce.setFocusable(false);
        btnVisuEnce.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnVisuEnce.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jtbSupEnc, org.jdesktop.beansbinding.ELProperty.create("false"), btnVisuEnce, org.jdesktop.beansbinding.BeanProperty.create("borderPainted"));
        bindingGroup.addBinding(binding);

        btnVisuEnce.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVisuEnceActionPerformed(evt);
            }
        });
        jtbSupEnc.add(btnVisuEnce);

        pnlEncJtb.add(jtbSupEnc, java.awt.BorderLayout.CENTER);

        pnlEnceSuperior.add(pnlEncJtb, java.awt.BorderLayout.WEST);

        pnlEncSupCent.setLayout(new javax.swing.BoxLayout(pnlEncSupCent, javax.swing.BoxLayout.LINE_AXIS));

        pnlEncDivisor.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        pnlEncDivisor.setMaximumSize(new java.awt.Dimension(1000, 20));
        pnlEncDivisor.setOpaque(false);
        pnlEncDivisor.setPreferredSize(new java.awt.Dimension(1000, 20));
        pnlEncSupCent.add(pnlEncDivisor);

        pnlEnceSuperior.add(pnlEncSupCent, java.awt.BorderLayout.CENTER);

        pnlEncerrados.add(pnlEnceSuperior, java.awt.BorderLayout.PAGE_START);
        pnlEncerrados.add(pnlEnceLeste, java.awt.BorderLayout.EAST);
        pnlEncerrados.add(pnlEnceOeste, java.awt.BorderLayout.WEST);
        pnlEncerrados.add(pnlEnceSul, java.awt.BorderLayout.SOUTH);

        tblEncerrados.setModel(controller.getChamadosEncerradosTableModel());
        tblEncerrados.setRowHeight(20);
        ajustarColunasEncerrados();
        tblEncerrados.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblEncerradosMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblEncerrados);

        pnlEncerrados.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jTabbedPane1.addTab("Encerrados", pnlEncerrados);

        getContentPane().add(jTabbedPane1, java.awt.BorderLayout.CENTER);

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAutoAtribuirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAutoAtribuirActionPerformed
        autoAtribuir();
    }//GEN-LAST:event_btnAutoAtribuirActionPerformed

    private void btnInserirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnInserirActionPerformed
        atribuirTecnico();
    }//GEN-LAST:event_btnInserirActionPerformed

    private void btnVisualizarNAtribuidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVisualizarNAtribuidoActionPerformed
        visualizarChamadoNAtri();
    }//GEN-LAST:event_btnVisualizarNAtribuidoActionPerformed

    private void tblNAtribuidosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblNAtribuidosMouseClicked
        if(evt.getClickCount() == 2){
            visualizarChamadoNAtri();
        }
    }//GEN-LAST:event_tblNAtribuidosMouseClicked

    private void tblNAtribuidosKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tblNAtribuidosKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
           visualizarChamadoNAtri();
        }
    }//GEN-LAST:event_tblNAtribuidosKeyPressed

    private void btnEncerrarNAtriActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEncerrarNAtriActionPerformed
        encerrarChamadoNAtri();
    }//GEN-LAST:event_btnEncerrarNAtriActionPerformed

    private void btnVisuEnceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVisuEnceActionPerformed
        visualizarChamadoEnce();
    }//GEN-LAST:event_btnVisuEnceActionPerformed

    private void btnResponderEAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResponderEAActionPerformed
        responderChamadoEA();
    }//GEN-LAST:event_btnResponderEAActionPerformed

    private void btnEncerrarEAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEncerrarEAActionPerformed
        encerrarChamadoEA();
    }//GEN-LAST:event_btnEncerrarEAActionPerformed

    private void btnIniciarIntervencaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIniciarIntervencaoActionPerformed
        int ret = JOptionPane.showConfirmDialog(null, "Deseja iniciar uma intervenção para este chamado?", 
                                                                "Intervenção", JOptionPane.YES_NO_OPTION);
        if (ret == JOptionPane.YES_OPTION) {
            iniciarIntervencao();
        }        
    }//GEN-LAST:event_btnIniciarIntervencaoActionPerformed

    private void btnVisualizarEAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVisualizarEAActionPerformed
        visualizarChamadoEA();
    }//GEN-LAST:event_btnVisualizarEAActionPerformed

    private void btnEncerrarIntervencaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEncerrarIntervencaoActionPerformed
        finalizarIntervencao();
    }//GEN-LAST:event_btnEncerrarIntervencaoActionPerformed

    private void tblEncerradosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblEncerradosMouseClicked
        if(evt.getClickCount() == 2){
            visualizarChamadoEnce();
        }        
    }//GEN-LAST:event_tblEncerradosMouseClicked
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAutoAtribuir;
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnBuscarEA;
    private javax.swing.JButton btnBuscarEnc;
    private javax.swing.JButton btnEncerrarEA;
    private javax.swing.JButton btnEncerrarIntervencao;
    private javax.swing.JButton btnEncerrarNAtri;
    private javax.swing.JButton btnIniciarIntervencao;
    private javax.swing.JButton btnInserir;
    private javax.swing.JButton btnResponderEA;
    private javax.swing.JButton btnVisuEnce;
    private javax.swing.JButton btnVisualizarEA;
    private javax.swing.JButton btnVisualizarNAtribuido;
    private javax.swing.JComboBox cbbBusca;
    private javax.swing.JComboBox cbbBuscaEA;
    private javax.swing.JComboBox cbbBuscaEnc;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JToolBar jtbSupEnc;
    private javax.swing.JToolBar jtbSuperior;
    private javax.swing.JToolBar jtbSuperiorEA;
    private javax.swing.JPanel pnlBusca;
    private javax.swing.JPanel pnlEABusca;
    private javax.swing.JPanel pnlEAJtb;
    private javax.swing.JPanel pnlEALeste;
    private javax.swing.JPanel pnlEAOeste;
    private javax.swing.JPanel pnlEASul;
    private javax.swing.JPanel pnlEASupCen;
    private javax.swing.JPanel pnlEASuperior;
    private javax.swing.JPanel pnlEmAndamento;
    private javax.swing.JPanel pnlEncBusca;
    private javax.swing.JPanel pnlEncDivisor;
    private javax.swing.JPanel pnlEncJtb;
    private javax.swing.JPanel pnlEncSupCent;
    private javax.swing.JPanel pnlEnceLeste;
    private javax.swing.JPanel pnlEnceOeste;
    private javax.swing.JPanel pnlEnceSul;
    private javax.swing.JPanel pnlEnceSuperior;
    private javax.swing.JPanel pnlEncerrados;
    private javax.swing.JPanel pnlJtb;
    private javax.swing.JPanel pnlNAtriLeste;
    private javax.swing.JPanel pnlNAtriOeste;
    private javax.swing.JPanel pnlNAtriSul;
    private javax.swing.JPanel pnlNAtriSuperior;
    private javax.swing.JPanel pnlNAtribuidos;
    private javax.swing.JPanel pnlSupCenDiEA;
    private javax.swing.JPanel pnlSuperiorCentro;
    private javax.swing.JPanel pnlSuperiorCentroDivisor;
    private javax.swing.JTable tblEA;
    private javax.swing.JTable tblEncerrados;
    private javax.swing.JTable tblNAtribuidos;
    private javax.swing.JTextField txtBusca;
    private javax.swing.JTextField txtBuscaEA;
    private javax.swing.JTextField txtBuscaEnc;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
   
     
    private void encerrarChamadoNAtri() {
        Chamado chamado = controller.getChamadosNAtriTableModel().getChamadoEJB(getLinha(tblNAtribuidos));
        JDFinalizarAdmin finalizar = new JDFinalizarAdmin(null, true, chamado, false, usuarioLogado);
    }
    
    private void encerrarChamadoEA() {
        Chamado chamado = controller.getChamadosEATableModel().getChamado(getLinha(tblEA));
                
        boolean encerrar = false;
        
        if (chamado.getRespostas() == null) {
            ArrayList<Resposta> respostas = new ArrayList<Resposta>(); 
            chamado.setRespostas(respostas);
        }
        
        if (!chamado.getRespostas().isEmpty()) {
            Resposta resposta = chamado.getRespostas().get(chamado.getRespostas().size()-1);  
            if (resposta.isAgindo()) {
                JOptionPane.showMessageDialog(null, "Este chamado está em intervenção técnica e não pode ser encerrado.");
            } else {
                encerrar = true;
            }         
        } else {
            encerrar = true;
        } 
        
        if (encerrar == true) {
            JDFinalizarAdmin finalizar = new JDFinalizarAdmin(null, true, chamado, true, usuarioLogado);
        }
        
    }
    
    private void visualizarChamadoEA() {
        Chamado chamado = controller.getChamadosEATableModel().getChamado(getLinha(tblEA));
        JDVisualizarChamAtr visualizarChamado = new JDVisualizarChamAtr(null, 
                                true, false, chamado, usuarioLogado);
    }        
      
    
    private void visualizarChamadoNAtri() {
        Chamado chamado = controller.getChamadosNAtriTableModel().getChamadoEJB(getLinha(tblNAtribuidos));
        JDVisualizarChamAdminNA visualizarChamado = new JDVisualizarChamAdminNA(null, true, chamado);
    }
    
    private void visualizarChamadoEnce() {
        Chamado chamado = controller.getChamadosEncerradosTableModel().getChamadoEJB(getLinha(tblEncerrados));
        JDVisualizarChamAtr visualizarChamado = new JDVisualizarChamAtr(null, true, true, chamado, usuarioLogado);
    }
    
    private void autoAtribuir() {
        int n = JOptionPane.showConfirmDialog(null,
                    "Deseja se autoatribuir a este chamado?",
                    "Atribuição de chamado",
                    JOptionPane.YES_NO_OPTION);
        
        if (n == JOptionPane.YES_OPTION) {
            try {
                controller.autoAtribuirChamadoAdmin(getLinha(tblNAtribuidos), this.usuarioLogado);                
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage());
            }
        }        
    }
    
    private void atribuirTecnico() {
        Chamado chamado = controller.getChamadosNAtriTableModel().getChamadoEJB(getLinha(tblNAtribuidos));
        JDAtribuirTecnico jdTecnico = new JDAtribuirTecnico(null, true, usuarioLogado,chamado);
    }
    
    private void iniciarIntervencao() {
        Chamado chamado = controller.getChamadosEATableModel().getChamado(getLinha(tblEA));
        boolean iniciarIntervencao = false;
        
        if (chamado.getRespostas().isEmpty()) {
            iniciarIntervencao = true;            
        } 
        
        else {    
            Resposta resposta = chamado.getRespostas().get(chamado.getRespostas().size()-1);  
            if (!resposta.isAgindo()) {
                iniciarIntervencao = true;
            } else {
                JOptionPane.showMessageDialog(null, "Este chamado está em intervenção técnica!");
            }
        }
        
        if (iniciarIntervencao == true) {
            try {                
                controller.iniciarIntervencao(chamado, getLinha(tblEA), usuarioLogado);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Um erro ocorreu! +\n" + ex.getMessage());
                Logger.getLogger(JIFChamadoAdmin.class.getName()).log(Level.SEVERE, null, ex);
            }        
        }        
    }
    
    private void finalizarIntervencao() {
        Chamado chamado = controller.getChamadosEATableModel().getChamado(getLinha(tblEA));
                
        if (!chamado.getRespostas().isEmpty()) {
            Resposta resposta = chamado.getRespostas().get(chamado.getRespostas().size()-1);  
            if (resposta.isAgindo()) {
                JDResponderEAAdmin jdResponderEAAdmin = new JDResponderEAAdmin(null, false, 
                                true, chamado, usuarioLogado, resposta, getLinha(tblEA));
            } else {
                JOptionPane.showMessageDialog(null, "Este chamado não possui nenhuma intervenção em aberto.");
            }         
        } else {
            JOptionPane.showMessageDialog(null, "Este chamado não possui nenhuma intervenção em aberto.");
        } 
    }       

    private void responderChamadoEA() {
        Chamado chamado = controller.getChamadosEATableModel().getChamado(getLinha(tblEA));
        boolean responder = false;
                
        if (!chamado.getRespostas().isEmpty()) {
            if (chamado.getRespostas().get(chamado.getRespostas().size()-1).isAgindo()) {
                responder = false;
            } else {
                responder = true;                
            }
        } else {
            responder = true;            
        }
        
        if (responder == true) {
            JDResponderEAAdmin jdResponderEAAdmin = new JDResponderEAAdmin(null, false, false, chamado, usuarioLogado);
        } else {
            JOptionPane.showMessageDialog(null, "Este chamado está em intervenção técnica!");
        }
        
    }   
    
    private int getLinha(JTable tbl) {
        return tbl.convertRowIndexToModel(tbl.getSelectedRow());
    }    
    
    private void ajustarColunasNaoAtribuido() {     
        TableCellRenderer tcr = new CellRenderer();  
        
        tblNAtribuidos.getColumnModel().getColumn(0).setCellRenderer(tcr);
        tblNAtribuidos.getColumnModel().getColumn(1).setCellRenderer(tcr);
        tblNAtribuidos.getColumnModel().getColumn(2).setCellRenderer(tcr);
        
        tblNAtribuidos.getColumnModel().getColumn(0).setMaxWidth(70);
        tblNAtribuidos.getColumnModel().getColumn(1).setMaxWidth(130);
        tblNAtribuidos.getColumnModel().getColumn(1).setMinWidth(110);
        tblNAtribuidos.getColumnModel().getColumn(2).setMinWidth(80);
        tblNAtribuidos.getColumnModel().getColumn(2).setMaxWidth(150);
        tblNAtribuidos.getColumnModel().getColumn(3).setMinWidth(180);
        tblNAtribuidos.getColumnModel().getColumn(3).setMaxWidth(170);        
    }
    
    private void ajustarColunasEncerrados() {     
        TableCellRenderer tcr = new CellRenderer();  
        
        tblEncerrados.getColumnModel().getColumn(0).setCellRenderer(tcr);
        tblEncerrados.getColumnModel().getColumn(1).setCellRenderer(tcr);
        tblEncerrados.getColumnModel().getColumn(2).setCellRenderer(tcr);
        
        tblEncerrados.getColumnModel().getColumn(0).setMaxWidth(70);
        tblEncerrados.getColumnModel().getColumn(1).setMaxWidth(80);
        tblEncerrados.getColumnModel().getColumn(1).setMinWidth(150);
        tblEncerrados.getColumnModel().getColumn(2).setMinWidth(90);
        tblEncerrados.getColumnModel().getColumn(2).setMaxWidth(150);
        tblEncerrados.getColumnModel().getColumn(3).setMinWidth(180);
        tblEncerrados.getColumnModel().getColumn(3).setMaxWidth(170);
        tblEncerrados.getColumnModel().getColumn(4).setMinWidth(180);
        tblEncerrados.getColumnModel().getColumn(4).setMaxWidth(170);    
    }
   
    private void ajustarColunasEmAndamento() {     
        TableCellRenderer tcr = new CellRenderer();  
                
        tblEA.getColumnModel().getColumn(0).setCellRenderer(tcr);
        tblEA.getColumnModel().getColumn(1).setCellRenderer(tcr);
        tblEA.getColumnModel().getColumn(2).setCellRenderer(tcr);
        
        tblEA.getColumnModel().getColumn(0).setMaxWidth(70);
        tblEA.getColumnModel().getColumn(1).setMaxWidth(120);
        tblEA.getColumnModel().getColumn(1).setMinWidth(150);
        tblEA.getColumnModel().getColumn(2).setMinWidth(120);
        tblEA.getColumnModel().getColumn(2).setMaxWidth(150);
        tblEA.getColumnModel().getColumn(3).setMinWidth(180);
        tblEA.getColumnModel().getColumn(3).setMaxWidth(170);
        tblEA.getColumnModel().getColumn(4).setMinWidth(180);
        tblEA.getColumnModel().getColumn(4).setMaxWidth(170);    
    }
}
